package model.gameImpl;

import java.util.ArrayList;
import java.util.List;

import model.Agent;
import model.Game;
import model.Property;
import model.StrategyType;

public class GameV3 extends Game {
	public GameV3() {
		super();
		setProperty(Property.GAME_NAME, "Evolutionary Game V3");
		setProperty(Property.GENERATIONS, 1000);
		setProperty(Property.INIT_AGENTS, 100);
		setProperty(Property.INIT_REPUTATION, 0.0);
		setProperty(Property.COST, 5);
		setProperty(Property.BENEFIT, 5);
		setProperty(Property.HISTORY_SIZE, 10);
		setProperty(Property.GROUP_SIZE, 2);
		setProperty(Property.BENEFIT_THRESHOLD, 1);
	}
	
	@Override
	public void initialize() {
		/*int tmpSize = (int)getProperty(Property.INIT_AGENTS)/11;
		
		for (int i=0 ; i < tmpSize ; i++)
			agents.add(new Agent(0.0, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize ; i < tmpSize*2 ; i++)
			agents.add(new Agent(0.1, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*2 ; i < tmpSize*3 ; i++)
			agents.add(new Agent(0.2, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*3 ; i < tmpSize*4 ; i++)
			agents.add(new Agent(0.3, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*4 ; i < tmpSize*5 ; i++)
			agents.add(new Agent(0.4, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*5 ; i < tmpSize*6 ; i++)
			agents.add(new Agent(0.5, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*6 ; i < tmpSize*7 ; i++)
			agents.add(new Agent(0.6, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*7 ; i < tmpSize*8 ; i++)
			agents.add(new Agent(0.7, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*8 ; i < tmpSize*9 ; i++)
			agents.add(new Agent(0.8, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*9 ; i < tmpSize*10 ; i++)
			agents.add(new Agent(0.9, (int)getProperty(Property.HISTORY_SIZE)));
		for (int i=tmpSize*10 ; i < tmpSize*11 ; i++)
			agents.add(new Agent(1.0, (int)getProperty(Property.HISTORY_SIZE)));*/
		
		System.out.println("Game " + this.getClass().getName() + " initialized");
	}

	@Override
	public void interact() {
		// Agents interact in the prisoners dilemma game.
		int groupSize = (int) getProperty(Property.GROUP_SIZE);
		double repetition = agents.size() / groupSize;

		for (int u = 0; u < repetition; u++) {
			ArrayList<Agent> l = new ArrayList<Agent>();

			for (int i = 0; i < groupSize; i++) {
				l.add(getRandomAgent());
			}

			// Calculates whether the group is granted a reward.
			int coopNum = 0;
			for (Agent a : l) {
				List<Agent> tmpList = l;
				tmpList.remove(a);
				if (a.determineStragedy(tmpList) == StrategyType.cooperator) {
					a.pay((int) getProperty(Property.GROUP_SIZE));
					a.history.Push(StrategyType.cooperator);
					coopNum++;
				}
				else {
					a.history.Push(StrategyType.defector);
				}
			}

			// Agents receive their reward.
			if (coopNum >= (int) getProperty(Property.BENEFIT_THRESHOLD)) {
				reward(l);
			}
		}
	}

	
	
	@Override
	public void reward() {
		// TODO Auto-generated method stub

	}

	@Override
	public void reproduce() {
		// TODO Auto-generated method stub

	}

	@Override
	public void output() {
		// TODO Auto-generated method stub

	}

}
